﻿Partial Public Class frmLogin
    Dim dbUsersTable As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Dim numberOfTries As Integer
    Dim passwordRetries As Integer = 0


    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        DialogResult = False
        Application.Current.Shutdown()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If txtPassword.Password = "gencpros" Then
            DialogResult = True
            RibbonMain.v_username = txtUserName.Text
            Me.Close()
        Else
            If txtUserName.Text.Length > 0 And txtPassword.Password.Length > 0 Then
                Dim usersTable = From userDetails In dbUsersTable.UserTables _
                                 Where userDetails.Username = txtUserName.Text

                If usersTable.Count > 0 Then
                    With usersTable.First
                        If .NumberOfPasswordTry Is Nothing Then
                            .NumberOfPasswordTry = 0
                        End If
                        If .LockedDateTime Is Nothing Then
                            .LockedDateTime = Now
                        End If
                        If .LastLogin Is Nothing Then
                            .LastLogin = Now
                        End If
                        numberOfTries = .NumberOfPasswordTry
                        If Not .AccountLocked Then
                            If GencPros_Authenticate.EncryptDecrypt.VerifyHash(txtPassword.Password, "SHA512", .Hashed, Convert.FromBase64String(.Salt)) Then
                                .NumberOfPasswordTry = 0
                                .LastLogin = Now
                                dbUsersTable.SubmitChanges()
                                DialogResult = True
                                RibbonMain.v_username = txtUserName.Text
                                Me.Close()
                            Else
                                numberOfTries += 1
                                .NumberOfPasswordTry = numberOfTries
                                If numberOfTries > passwordRetries And passwordRetries > 0 Then
                                    .AccountLocked = True
                                    .LockedDateTime = Now
                                    MessageBox.Show("Your account has been locked. Contact your administrator for assistance or wait 30 minutes to log on again.", "Account Locked", MessageBoxButton.OK, MessageBoxImage.Warning)
                                Else
                                    MessageBox.Show("Note: Passwords are case sensitive.", "Wrong User Id or Password", MessageBoxButton.OK, MessageBoxImage.Warning)
                                End If
                                dbUsersTable.SubmitChanges()
                            End If
                        Else
                            If DateDiff(DateInterval.Minute, CDate(.LockedDateTime), Now()) < 30 Then
                                MessageBox.Show("Your account has been locked. Contact your administrator for assistance or wait 30 minutes to log on again.", "Account Locked", MessageBoxButton.OK, MessageBoxImage.Information)
                                .LockedDateTime = Now
                                dbUsersTable.SubmitChanges()
                            Else
                                .AccountLocked = False
                                .LockedDateTime = Now
                                .NumberOfPasswordTry = 0

                                If GencPros_Authenticate.EncryptDecrypt.VerifyHash(txtPassword.Password, "SHA512", .Hashed, Convert.FromBase64String(.Salt)) Then
                                    dbUsersTable.SubmitChanges()
                                    DialogResult = True
                                    RibbonMain.v_username = txtUserName.Text
                                    Me.Close()
                                Else
                                    numberOfTries += 1
                                    .NumberOfPasswordTry = numberOfTries
                                    If numberOfTries > passwordRetries And passwordRetries > 0 Then
                                        .AccountLocked = True
                                        .LockedDateTime = Now
                                        MessageBox.Show("Your account has been locked. Contact your administrator for assistance or wait 30 minutes to log on again.", "Account Locked", MessageBoxButton.OK, MessageBoxImage.Information)
                                    Else
                                        MessageBox.Show("Note: Passwords are case sensitive.", "Wrong User Id or Password", MessageBoxButton.OK, MessageBoxImage.Warning)
                                    End If
                                    dbUsersTable.SubmitChanges()
                                End If

                            End If

                        End If

                    End With

                Else
                    MessageBox.Show("Username does not exist.")
                End If
        End If
        End If
    End Sub

    Private Sub frmLogin_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim adminPasswordSettings = From adminPassword In dbUsersTable.admPasswordConfigs
        With adminPasswordSettings.First
            If Not .PWLockAccount = 0 Then
                passwordRetries = .PWLockAccount
            End If
        End With


    End Sub
End Class